<template>
  <el-dialog :title="formData.id?'编辑页面':'添加页面'" :visible="addShowDialog" height="80%" @close="closeFn">
    <el-form ref="form" label-width="80px" :rules="formRules" :model="formData">
      <el-form-item label="标题" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="formData.content" @blur="$refs.form.validateField('content')" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row>
        <el-button type="primary" size="small" @click="comfirmFn">确认</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
// import 'quill/dist/quill.bubble.css'
import 'quill/dist/quill.bubble.css'
// 引入新增,编辑页面接口
import { PagesAdd, PagesEdit } from '@/api/pages'
export default {
  components: {
    'quill-editor': quillEditor
  },
  props: {
    addShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formRules: {
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      formData: {
        name: '', // 名称
        content: '', // 内容
        id: ''
      }

    }
  },
  methods: {
    async comfirmFn() {
      // 效验
      await this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑接口
        await PagesEdit(this.formData)
      } else {
        // 调用新增接口
        await PagesAdd(this.formData)
      }
      // console.log(57, res)
      // 关闭弹窗
      this.closeFn()
      this.$emit('updateEvent')
    },
    closeFn() {
      // 清空效验
      this.$refs.form.resetFields()
      // 清空表单数据
      this.formData = {
        name: '', // 名称
        content: '' // 内容
      }
      // 关闭弹窗
      this.$emit('update:addShowDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .ql-editor{
  height: 200px;
}

</style>
